/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include <stdint.h>
#include "at32_mcu_info.h"
#include "bootloader.h"
#include "dev_info.h"
#include "zino_products.h"
#include "zino.h"

#include <stdio.h>

// 将月份字符串转换为数字
#define MONTH_TO_INT(month) ( \
    (month[0] == 'J' && month[1] == 'a' && month[2] == 'n') ? 1 : \
    (month[0] == 'F' && month[1] == 'e' && month[2] == 'b') ? 2 : \
    (month[0] == 'M' && month[1] == 'a' && month[2] == 'r') ? 3 : \
    (month[0] == 'A' && month[1] == 'p' && month[2] == 'r') ? 4 : \
    (month[0] == 'M' && month[1] == 'a' && month[2] == 'y') ? 5 : \
    (month[0] == 'J' && month[1] == 'u' && month[2] == 'n') ? 6 : \
    (month[0] == 'J' && month[1] == 'u' && month[2] == 'l') ? 7 : \
    (month[0] == 'A' && month[1] == 'u' && month[2] == 'g') ? 8 : \
    (month[0] == 'S' && month[1] == 'e' && month[2] == 'p') ? 9 : \
    (month[0] == 'O' && month[1] == 'c' && month[2] == 't') ? 10 : \
    (month[0] == 'N' && month[1] == 'o' && month[2] == 'v') ? 11 : \
    (month[0] == 'D' && month[1] == 'e' && month[2] == 'c') ? 12 : 0 \
)

// 解析 __DATE__ 并提取年、月、日
#define GET_YEAR(date) ( \
    (date[7] - '0') * 1000 + (date[8] - '0') * 100 + \
    (date[9] - '0') * 10 + (date[10] - '0') \
)

#define GET_MONTH(date) MONTH_TO_INT(date)

#define GET_DAY(date) ( \
    (date[4] == ' ' ? 0 : date[4] - '0') * 10 + (date[5] - '0') \
)

/**
 * @brief 固件信息，用于固件升级，否则升级工具无法识别固件。
 *
 */
const firmware_info_t zino_firemware_info __attribute__((section("firemware_info"))) = {
    .infoCrc = ~FLASH_FW_MAGIC,
    .magic = FLASH_FW_MAGIC,
    .start_addr = FLASH_APP_ADDR, // 固件起始地址
    .size = 0,
    .FWcrc = 0,
    .version = VESION2INT(DEV_MVID, DEV_SVID, DEV_BVID, 1), // 固件版本号
    .pid = ZINO_STAR_AVIATION_BALL_DRONE,                   // 产品ID
    .name = "星穹航空-球形无人机",
    .date = DATE2INT(GET_YEAR(__DATE__), GET_MONTH(__DATE__), GET_DAY(__DATE__)),
    // .date = DATE2INT(DEV_YEAR, DEV_MONTH, DEV_DAY),         // 固件生成日期
    // .date = DATE2INT(DEV_YEAR, DEV_MONTH, DEV_DAY),         // 固件生成日期
    .DeviceID = AT32F413RCT7_256KB_LQFP64, // 产品主控型号

};
uint32_t get_dev_pid(void)
{
    return zino_firemware_info.pid;
}
uint32_t get_dev_vid(void)
{
    return zino_firemware_info.version;
}
int show_dev_version(void)
{
    rt_kprintf("\n\nZINO Board info:\n");
    rt_kprintf("    *Name: %s\n", zino_firemware_info.name);
    rt_kprintf("    *DeviceID: [%08X%08X]\n", (uint32_t)(zino_firemware_info.DeviceID>>32),(uint32_t)zino_firemware_info.DeviceID);
    rt_kprintf("    *PID: %d.%d,%d.%d\n", INFO_BOARD(zino_firemware_info), INFO_CORE(zino_firemware_info), INFO_PRODUCT(zino_firemware_info), INFO_FUNCTION(zino_firemware_info));
    rt_kprintf("    *VID: %d.%d.%d.%d\n", INFO_MAIN(zino_firemware_info), INFO_SUB(zino_firemware_info), INFO_BUILD(zino_firemware_info), INFO_NUM(zino_firemware_info));
    rt_kprintf("    *Date: %4d.%2d.%2d\n", INFO_YEAR(zino_firemware_info), INFO_MONTH(zino_firemware_info), INFO_DAY(zino_firemware_info));
    rt_kprintf("    *\n\n");
    return 0;
}
// ZINO_BOARD_EXPORT(show_dev_version);
